package com.yaoandw.storage;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import com.sleepycat.bind.EntryBinding;
import com.sleepycat.bind.serial.SerialBinding;
import com.sleepycat.collections.StoredMap;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.EnvironmentMutableConfig;
import com.yaoandw.util.Md5Encode;

public class BDBFrontier extends AbstractFrontier implements Frontier {

	private StoredMap<String,SimpleCrawlUrl> pendingUrisDB = null;

	// 使用默认的路径和缓存大小构造函数
	public BDBFrontier(String homeDirectory) throws DatabaseException,
			FileNotFoundException {
		super(homeDirectory);
		EntryBinding<String> keyBinding = new SerialBinding<String>(javaCatalog, String.class);
		EntryBinding<SimpleCrawlUrl> valueBinding = new SerialBinding<SimpleCrawlUrl>(javaCatalog,
				SimpleCrawlUrl.class);
		pendingUrisDB = new StoredMap<String,SimpleCrawlUrl>(database, keyBinding, valueBinding, true);
	}
	public List<SimpleCrawlUrl> list(){
		List<SimpleCrawlUrl> result = new ArrayList<SimpleCrawlUrl>();
		if (!pendingUrisDB.isEmpty()) {
			for(Iterator<Entry<String,SimpleCrawlUrl>> it = pendingUrisDB.entrySet().iterator();it.hasNext();){
				result.add(it.next().getValue());
			}
		}
		return result;
	}
	// 获得下一条记录
	public SimpleCrawlUrl getNext() {
		SimpleCrawlUrl result = null;
		if (!pendingUrisDB.isEmpty()) {
//			Set entrys = pendingUrisDB.entrySet();
//			System.out.println(entrys);
			Entry<String,SimpleCrawlUrl> entry = (Entry<String,SimpleCrawlUrl>) pendingUrisDB
					.entrySet().iterator().next();
			result = entry.getValue();
			delete(entry.getKey());
		}
		return result;
	}

	// 存入URL
	public SimpleCrawlUrl putUrl(SimpleCrawlUrl url) {
		return put(caculateUrl(url.getOriUrl()), url);
	}

	// 存入数据库的方法
	protected synchronized SimpleCrawlUrl put(String key, SimpleCrawlUrl value) {
		if(get(key) == null && !FrontierFactory.getVisitedFrontier().contains(value.getOriUrl())){
			pendingUrisDB.put(key, value);
			return value;
		}
		return null;
	}

	// 取出
	protected SimpleCrawlUrl get(String key) {
		return pendingUrisDB.get(key);
	}

	// 删除
	protected SimpleCrawlUrl delete(String key) {
		return pendingUrisDB.remove(key);
	}

	// 根据URL计算键值，可以使用各种压缩算法，包括MD5等压缩算法
	private String caculateUrl(String url) {
		return Md5Encode.encode(url);
	}

	// 测试函数
	public static void main(String[] strs) {

		try {
			BDBFrontier bBDBFrontier = new BDBFrontier("d:/work/bdb");
			
			EnvironmentMutableConfig emc = bBDBFrontier.getEnv().getMutableConfig();
			System.out.println(emc.getCacheSize());
			System.out.println(emc.getCachePercent());
			
			CrawlUrl url = new CrawlUrl();
			url.setOriUrl("http://www.163.com");
			bBDBFrontier.putUrl(url);
			System.out.println(((CrawlUrl) bBDBFrontier.getNext()).getOriUrl());
			bBDBFrontier.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}

	}

}
